//
//  WPSingleStatusViewModel.swift
//  WB
//
//  Created by 汪鹏 on 2017/1/6.
//  Copyright © 2017年 汪鹏. All rights reserved.
//  单条微博视图模型(在这里处理单条微博的逻辑)

import Foundation

/*
 如果没有任何父类，在开发调试，打印输出信息，需要：
    1.遵守 CustomStringConvertible 协议
    2. 实现 description 计算型属性
 
 */
class WPSingleWBViewModel: CustomStringConvertible {
    
    // 微博数据
    var statu:WPWBStatus
    
    // 会员图片
    var vipIcon:UIImage?
    
    // 认证图片
    var verifierIcon:UIImage?
    
    // 转发文字
    var repostString:String?
    
    // 评论文字
    var commentString:String?
    
    // 点赞文字
    var attitudesString:String?
    
    // 计算配图视图
    var pic_urls:[WPStatusPictures]?{
        return statu.retweeted_status?.pic_urls ?? statu.pic_urls
    }
    
    // 转发文本
    var retweeted_statu_text:String?
    
    // 配图视图的高度
    var pictureViewSize:CGSize = CGSize()
    
    // 微博来源
    var sourceText:String?
    
    // 缓存行高
    var rowHeight:CGFloat = 0
    
    init(model:WPWBStatus) {
        statu = model
        
        if let level = model.user?.mbrank {
            
            vipIcon = UIImage(named: "common_icon_membership_level\(level)")
            
        }
        
        switch model.user?.verified_type ?? -1 {
        case 0:
            verifierIcon = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifierIcon = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifierIcon = UIImage(named: "avatar_grassroot")
        default:
            break
        }
        
        // 计算底部工具栏内容
        repostString = calculateCount(count: model.reposts_count, defaultString: "转发")
        
        commentString = calculateCount(count: model.reposts_count, defaultString: "评论")
        
        attitudesString = calculateCount(count: model.reposts_count, defaultString: "点赞")
        
        // 计算配图大小
        pictureViewSize = calculatePictureViewSize(count: pic_urls?.count ?? 0 )
        
        retweeted_statu_text = "@" + (model.retweeted_status?.user?.screen_name ?? "")
            + ":"
            + (model.retweeted_status?.text ?? "")
        
        
        calculateRowHeight()
        
        //sourceText = "来自:" + (model.source?.regularHrefAndText()?.txt ?? "")
    }
    
    // 计算行高
    private func calculateRowHeight() {
       
        // 内部间距
        let margin:CGFloat = 12
        
        // 左右的间距
        let leftRightMargin:CGFloat = 11
        
        // 文本内容显示的最大区域
        let textSize:CGSize = CGSize(width: UIScreen.cz_screenWidth() - 2 * leftRightMargin, height: CGFloat(MAXFLOAT))
        
        // 图像高度
        let iconHeight:CGFloat = 34
        
        // 原创微博文字字体大小
        let originalFont:UIFont = UIFont.systemFont(ofSize: 15)
        
        // 转发微博文字字体大小
        let reweetlFont:UIFont = UIFont.systemFont(ofSize: 14)

        // 记录高度
        var height:CGFloat = 0
        
        // 底部视图的高度
        let toolBarHeight:CGFloat = 35
        
        // 计算顶部位置
        height += 2 * margin + iconHeight + margin
        
        // 计算正文的高度
        if let text = statu.text {
            
            height += (text as NSString).boundingRect(with: textSize,
                                                      options: [.usesLineFragmentOrigin],
                                                      attributes: [NSFontAttributeName:originalFont],
                                                      context: nil).height
        }
        
        // 转发微博
        if statu.retweeted_status != nil {
            
            // 加上间距
            height += margin + margin
            
            if let reweetText = retweeted_statu_text {
                
                // 计算转发文本的
                height += (reweetText as NSString).boundingRect(with: textSize,
                                                                options: [.usesLineFragmentOrigin],
                                                                attributes: [NSFontAttributeName:reweetlFont],
                                                                context: nil).height
            }
        }
        
        
        // 配图的高度
        height += pictureViewSize.height
        
        // 底部视图的高度
        height += toolBarHeight
        
        // 赋值给属性
        rowHeight = height
        
    }
    
    
    /// 更新配图视图大小
    ///
    /// - Parameter image: 图片
    func updatePictureSize(image:UIImage){
        
        var size = image.size
        
        // 针对图片过大/过小处理
        let maxSizeWidth:CGFloat = 300
        
        if size.width > maxSizeWidth {
            
            size.width = maxSizeWidth
            
             size.height = size.width * image.size.height / image.size.width
            
        }
        
        let minSizeWidth:CGFloat = 40
        
        if size.width < minSizeWidth  {
            
            size.width = minSizeWidth
            
            size.height = size.width * image.size.height / image.size.width
            
        }

        
        size.height += PictureViewTopMargin
        
        pictureViewSize = size
        
        calculateRowHeight()
        
    }
    
    /// 根据图片个数计算大小
    ///
    /// - Parameter count: 图片个数
    /// - Returns: 计算后的大小
    private func calculatePictureViewSize(count:Int) -> CGSize {
        
        if count == 0 {
            
            return CGSize()
            
        }
        
        // 根据个数计算行数
        let row = (count - 1) / 3 + 1
        
        // 配图视图高度
        let PictureViewHeight:CGFloat = PictureViewTopMargin + CGFloat(row - 1) * PictureViewInnerMargin + PictureViewItemHeight * CGFloat(row)
        
        return CGSize(width: PictureViewWidth, height: PictureViewHeight)
        
    }
    
    /// 计算底部视图内容
    ///
    /// - Parameters:
    ///   - count: 数字
    ///   - defaultString: 默认文字
    /// - Returns: 结果内容
    private func calculateCount(count:Int, defaultString:String) -> String {
       
        if count == 0 {
            return defaultString
        }
        
        if count < 10000 {
            return count.description
        }
        
        return String(format: "%.02f万", arguments: [Double(count)/10000])
    }
    
    var description: String{
        return statu.description

    }
}
